﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class adm_vlg_adminOrder : System.Web.UI.Page
{
    CommonPage cm3 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    //string cateId = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack) return;
        //LoadListCategory();
        fillDulieuLenGrid("");
        //tu dong hoan thanh hoac huy cac don hang dang cho
        AutoCancelOrderNotActive();
        AutoCompleteOrder();
    }
    //void LoadListCategory()
    //{
    //    ListItemCollection lst = new ListItemCollection();
    //    lst.Add(new ListItem { Value = "-1", Text = "--Tất cả--" });
    //    lst.Add(new ListItem { Value = "0", Text = "Mới khởi tạo" });
    //    lst.Add(new ListItem { Value = "1", Text = "Đã hủy" });
    //    lst.Add(new ListItem { Value = "2", Text = "Đã tiếp nhận" });
    //    lst.Add(new ListItem { Value = "3", Text = "Đã hoàn thành" });
    //    lst.Add(new ListItem { Value = "4", Text = "Đã thanh toán" });
    //    drp_cat.DataSource = lst;
    //    drp_cat.DataTextField = "Text";
    //    drp_cat.DataValueField = "Value";
    //    drp_cat.DataBind();
    //    try
    //    {
    //        drp_cat.Items.FindByValue(Session["catid"].ToString()).Selected = true;
    //    }
    //    catch { }
    //}
    //protected void drp_cat_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    string cateId = drp_cat.SelectedValue;
    //    fillDulieuLenGrid(cateId);
    //    // Session["catid"] = drp_cat.SelectedValue;
    //}

    void fillDulieuLenGrid(string cateId)
    {
        IQueryable<Order> list = null;
        if (Request.QueryString["id"] != null)
        {
            if (cateId != "-1" && cateId != "")
            {
                list = from gg2 in _db8.Orders
                       orderby gg2.NgayDat descending
                       where gg2.idGig == Convert.ToInt32(Request.QueryString["id"]) && gg2.Status == Convert.ToInt32(cateId)
                       select gg2;
            }
            else
            {
                list = from gg2 in _db8.Orders
                       orderby gg2.NgayDat descending
                       where gg2.idGig == Convert.ToInt32(Request.QueryString["id"])
                       select gg2;
            }

        }
        else
        {
            if (cateId != "-1" && cateId != "")
            {
                list = from gg2 in _db8.Orders
                       orderby gg2.NgayDat descending
                       where gg2.Status == Convert.ToInt32(cateId)
                       select gg2;
            }
            else
            {
                list = from gg2 in _db8.Orders
                       orderby gg2.NgayDat descending
                       select gg2;
            }
        }

        GridView1.DataSource = list;

        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        fillDulieuLenGrid("");
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    //Cập nhật ẩn hiện trên lưới
    public void chkAnHien_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAnHien = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkAnHien.NamingContainer;
        string cid = chkAnHien.ToolTip;
        bool status = chkAnHien.Checked;
        Category cate = _db8.Categories.SingleOrDefault(x => x.idCategory == int.Parse(cid));
        cate.AnHien = status;
        _db8.SubmitChanges();
        // fillDulieuLenGrid();
    }
    protected void linkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add"));
    }

    protected void linkEditG_Click(object sender, EventArgs e)
    {

    }
    
    protected void butt_save_Click(object sender, EventArgs e)
    {

        var list = from o in _db8.Orders
                   where o.CustomerActive != true && o.CustomerCancel != true && o.SellerCancel != true
                   select o;
        foreach (var item in list)
        {
            //TimeSpan current = (item.NgayDat.Value).AddDays(2) - DateTime.Now;
            TimeSpan current = item.NgayDat.Value.AddDays(2) - DateTime.Now;
            if (current.Hours < 0 )
            {
                item.CustomerCancel = true;
                item.SellerCancel = true;
                item.dateSellerCancel = DateTime.Now;
                item.dateCustomerCancel = DateTime.Now;
                var acc = (from a in _db8.VLGAccounts
                           where a.idAcc == item.idAcc
                           select a).Single();
                //thanh toán paypal --> tra so tien paypal
                if (item.Type == 0 || item.Type == 2)
                {
                    if (acc.soTienPaypal.HasValue)
                        acc.soTienPaypal = acc.soTienPaypal + 5;
                    else acc.soTienPaypal = 5;
                }
                //thanh toán truc tiep --> tra so du vao so tien
                else
                {
                    if (acc.soTien.HasValue)
                        acc.soTien = acc.soTien + 5;
                    else
                        acc.soTien = 5;
                }
                _db8.SubmitChanges();

            }
        }
    }
    //HỦY ĐƠN HÀNG SAU KHI HET THOI GIAN 2 NGAY VAN CHUA XÁC NHẬN QUA EMAIL
    public void AutoCancelOrderNotActive()
    {
        var list = from o in _db8.Orders
                   where o.CustomerActive != true && o.CustomerCancel != true && o.SellerCancel != true
                   select o;
        foreach (var item in list)
        {
            //TimeSpan current = (item.NgayDat.Value).AddDays(2) - DateTime.Now;
            TimeSpan current = item.NgayDat.Value.AddDays(2) - DateTime.Now;
            if (current.Hours < 0)
            {
                item.CustomerCancel = true;
                item.SellerCancel = true;
                item.dateSellerCancel = DateTime.Now;
                item.dateCustomerCancel = DateTime.Now;
                var acc = (from a in _db8.VLGAccounts
                           where a.idAcc == item.idAcc
                           select a).Single();
                //thanh toán paypal --> tra so tien paypal
                if (item.Type == 0 || item.Type == 2)
                {
                    if (acc.soTienPaypal.HasValue)
                        acc.soTienPaypal = acc.soTienPaypal + 5;
                    else acc.soTienPaypal = 5;
                }
                //thanh toán truc tiep --> tra so du vao so tien
                else
                {
                    if (acc.soTien.HasValue)
                        acc.soTien = acc.soTien + 5;
                    else
                        acc.soTien = 5;
                }
                _db8.SubmitChanges();

            }
        }
    }

    //HOÀN THÀNH ĐƠN HÀNG SAU KHI HET THOI GIAN 3 NGAY VAN CHUA XÁC NHẬN 
    public void AutoCompleteOrder()
    {
        var list = from o in _db8.Orders
                   where (o.CustomerConfirm != true && o.SellerReconfirm == true && o.SellerReconfirm != true && o.CustomerReject != true)
                   || (o.CustomerConfirm != true && o.SellerReconfirm == true && o.CustomerReject == true)
                   select o;
        foreach (var item in list)
        {
            //hoan thanh reconfirm trước
            if (item.dateSellerReconfirm.HasValue)
            {
                TimeSpan current = item.dateSellerReconfirm.Value.AddDays(3) - DateTime.Now;
                if (current.Hours < 0)
                {
                    item.CustomerConfirm = true;
                    item.NgayCustomerConfirm = DateTime.Now;
                    _db8.SubmitChanges();

                }
            }
            else
            {
                TimeSpan current = item.NgaySellerConfirm.Value.AddDays(3) - DateTime.Now;
                if (current.Hours < 0)
                {
                    item.CustomerConfirm = true;
                    item.NgayCustomerConfirm = DateTime.Now;
                    _db8.SubmitChanges();

                }
            }
        }
    }
}
